package com.example.weex;

import android.app.Application;
import android.widget.Toast;

import com.alibaba.sdk.android.AlibabaSDK;
import com.alibaba.sdk.android.SdkConstants;
import com.alibaba.sdk.android.callback.InitResultCallback;
import com.aliyun.alink.business.alink.ALinkConfigure;
import com.aliyun.alink.sdk.net.anet.wsf.WSFConfigure;
import com.aliyun.alink.sdk.net.anet.wsf.WSFNet;
import com.aliyun.alink.utils.ALog;
import com.example.AliSDK.WeexLogin;
import com.taobao.weex.InitConfig;
import com.taobao.weex.WXEnvironment;
import com.taobao.weex.WXSDKEngine;
import com.taobao.weex.common.WXException;

import java.io.IOException;

/**
 * Created by Administrator on 2016-7-24.
 */
public class OpenAApplication extends Application {
    private String certName = "cacert.pem";
    @Override
    public void onCreate() {
        super.onCreate();
        //init Weex
        WXEnvironment.addCustomOptions("appName","TBSample");
        WXSDKEngine.initialize(this,new InitConfig.Builder().setImgAdapter(new ImageAdapter()).build());
        try {
            WXSDKEngine.registerModule("login", WeexLogin.class);
        } catch (WXException e) {
            e.printStackTrace();
        }

        //init OpenSDK
        AlibabaSDK.asyncInit(this, new InitResultCallback() {
            @Override
            public void onSuccess() {
                Toast.makeText(OpenAApplication.this, "初始化成功", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(OpenAApplication.this, "初始化失败"+s, Toast.LENGTH_LONG).show();
            }
        });

        SDKInit();
    }

    public  void SDKInit(){

        ALog.setLevel(ALog.LEVEL_DEBUG);

        //初始化长连接SSL
        try {
            WSFConfigure.wsfCertificationFile =  this.getAssets().open(this.certName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        WSFNet.init(getApplicationContext());

        String App_Key = AlibabaSDK.getGlobalProperty(SdkConstants.APP_KEY);
        System.out.print("================================="+App_Key);
        ALog.e("Alink","App_Key="+App_Key);

        ALinkConfigure.appKey = App_Key;
        ALinkConfigure.appVersion = "1.0.0";
        ALinkConfigure.alinkHost = "com.example.OpenADemo";
    }
}
